package com.tiheima.service.impl;

import com.tiheima.mapper.DeptMapper;
import com.tiheima.model.entity.Dept;
import com.tiheima.service.DeptService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DeptServiceImpl implements DeptService {
    private final DeptMapper deptMapper;

    public DeptServiceImpl(DeptMapper deptMapper) {
        this.deptMapper = deptMapper;
    }

    @Override
    public List<Dept> depts() {
        return deptMapper.selectDeptList();
    }

    @Override
    public void deleteDept(Integer id) {
        deptMapper.deleteDept(id);
    }

    @Override
    public void saveDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDept(dept);
    }

    @Override
    public Dept getDeptById(Integer id) {
        if (id == null || id < 1) {
            throw new RuntimeException("参数有误！");
        }
        return deptMapper.selectEdptById(id);
    }

    @Override
    public void updateDeptById(Dept dept) {
        if (Objects.isNull(dept) || Objects.isNull(dept.getId()) || Objects.isNull(dept.getName())){
            throw new RuntimeException("参数有误！");
        }
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDeptById(dept);
    }
}
